<?php

require_once("inc.member.php");



$this_title="$vars[member_title] &raquo; ".__("Select Stockist");

$content_title = $page_title=__("Select Stockist");



//#####EDIT STOCKIST POST#####

if($_POST["__req"]){

 $arr_stockist_id = split(",",$post_s['selected_id']);

 if(count($arr_stockist_id)<>5){

 	$errmsg=__("Please select five stockist code!");

 }

 

 if(!$errmsg){

  //check stockist id

  if(!$r_count=@mysql_fetch_assoc(mysql_query("select count(id) as ttl from $db->users where id in (".$post_s['selected_id'].") and stockist_uid=0"))){

    $errmsg.=format_err(__("Connection error, please retry!"))."<br />\n";

  }else{

  	if($r_count['ttl']==5){

  		$sql="update $db->users set stockist_uid='".$uid."',stockist_assign_date='".date('Y-m-d')."' where id in (".$post_s['selected_id'].")";

		if(!mysql_query($sql)){

	    	$errmsg.=format_err(__("Error updating the stockist record.")."<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error())."<br />\n";

	   	}

	   	

	   	if(!$errmsg){

	   		$sql="update $db->stockist_access set udate='".date('Y-m-d')."' where id=".$_GET['id'];

			if(!mysql_query($sql)){

		    	$errmsg.=format_err(__("Error updating the stockist record.")."<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error())."<br />\n";

		   	}

	   	}		   	

  	}else{

  		$errmsg.=format_err(__("Stockist code have been select by other user, please select again!"))."<br />\n";

  	}

  }

 }

 //#####END ERROR CHECK#####



 //#####UPDATE TO DB#####

 if(!$errmsg){  

   $forward_url=$return_url? $return_url : MEMBER_URL."/".$vars["file"]["member"]["view_stockist_sales"];

   header("Location: $forward_url");

 }



 $msg=$msg? format_msg($msg) : "";

 //$errmsg=$errmsg? format_err(__("There is some error(s), please correct them before continuing:")."<br />\n<br />\n$errmsg") : "";

}

//#####END STOCKIST ACCESS POST#####



//#####LIST MEMBER STOCKIST#####

$dist=mysql_fetch_assoc(mysql_query("select ref_left, ref_right from $db->users where id=17574"));

$left=$dist['ref_left'];

$right=$dist['ref_right'];

$count_sql="select count(*) from $db->users where ref_left between $left and $right and is_stockist='y' and stockist_code<>'' and stockist_uid=0";

$record_sql="select * from $db->users where ref_left between $left and $right and is_stockist='y' and stockist_code<>'' and stockist_uid=0 order by stockist_code";



$cur_page=$_GET["nav"];

$total_record=@mysql_result(mysql_query($count_sql), 0);

$total_page=ceil($total_record/$item_per_page);

$page=($cur_page? ($cur_page>$total_page? $total_page : ($cur_page<1? 1 : $cur_page)) : 1);

$limit_start=($page-1)*$item_per_page;

$nav_link=format_page($page, $total_page, 20);

$this_page_total=@mysql_num_rows($r=mysql_query($record_sql));

$k=0;



for($i=0;$i<$this_page_total;$i++){

	$run_no = (($page - 1) * $item_per_page) + $i + 1;

	$stockist=mysql_fetch_assoc($r);

	$stockist_row.="

	<tr class='row$k'>

		<td>$run_no</td>

		<td>".$stockist["stockist_code"]."</td>

		<td><input type='checkbox' name='id[]' id='id[]' value='".$stockist["id"]."'/></td>

	</tr>";

	$k=1-$k;

}



//javascript

$jvscript=

"

<script type='text/javascript'>

function ValidateStockist(){

	count=0;

	selected_sid='';

	for (counter = 0; counter < document.user_form.elements['id[]'].length; counter++)

	{		

		if (document.user_form.elements['id[]'][counter].checked)

		{

			if(selected_sid==''){

				selected_sid=document.user_form.elements['id[]'][counter].value;

			}else{

				selected_sid=selected_sid+','+document.user_form.elements['id[]'][counter].value;

			}

			count = count + 1;

		}

	}

	

	if(count>5){

		alert('Only can select five stockist code!');

		return false;

	}

	

	if(count<5){

		alert('Need to select five stockist code!');

		return false;

	}

	

	document.user_form.selected_id.value=selected_sid;

	return true;

}

</script>";

/*

//listing

$stockist_list=($errmsg || $msg? $errmsg.$msg : "").

"<p class='center'></p>

$search_ui<br />\n

$listing_text $nav_link

<table class='pbl_table'>

	<tr class='pbl_header'>		

		<td width='40' align='left'>".__("No")."</td>

		<td>".__("Stockist Code")."</td>

		<td width='40'>".__("Select")."</td>

	</tr>

	$sale_row

</table>

</form>";

*/

$stockist_list=($errmsg || $msg? $errmsg.$msg : "").

"<form name='aj_form' method='post' action='$this_file'></form>

<form name='user_form' method='post' action='$this_file?$_SERVER[QUERY_STRING]'>

<input type='hidden' name='__req' value='1' />

<input type='hidden' name='selected_id' value='' />

<table class='pbl_table'>

 <tr class='pbl_header'>		

		<td width='40' align='left'>".__("No")."</td>

		<td align='left'>".__("Stockist Code")."</td>

		<td width='40'>".__("Select")."</td>

 </tr>

 $stockist_row

 <tr>

  <td colspan='3' class='center' style='padding:20px 0 20px 0;'>

   <input type='submit' name='submit_btn' value=\"".__("Submit")."\" onclick='javascript:return ValidateStockist(); '/>

  </td>

</table>

</form>

$c_jvscript";

//#####END MEMBER STOCKIST LIST#####



$content="<h2>$page_title</h2>".$stockist_list;

//$content=str_replace("<%tab_content%>", $content, $tab);



print format_member_page($content, $this_title, $content_title, $css.$jvscript);



?>